package cn.tedu.zx.court.adapter.controller;


import cn.tedu.zx.common.restful.JsonPage;
import cn.tedu.zx.common.restful.JsonResult;
import cn.tedu.zx.common.restful.ResponseCode;
import cn.tedu.zx.court.service.CategoryService;
import cn.tedu.zx.pojo.court.dto.CategoryAddNewDto;
import cn.tedu.zx.pojo.court.dto.CategoryUpdateDto;
import cn.tedu.zx.pojo.court.vo.CategoryDetailsVo;
import cn.tedu.zx.pojo.court.vo.CategoryIdNameList;
import cn.tedu.zx.pojo.court.vo.CategoryListVo;
import cn.tedu.zx.pojo.court.vo.TreeListVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
//通过knife4j配置IPI文档
@Api(tags = "01.分类模块")
@Validated/*未封装参数检查注解*/
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping("/addNew")
    @ApiOperation("添加分类")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAnyAuthority('/category/insert')")
    /*@ApiIgnore注解忽略注解后的请求参数*/
    public JsonResult addNew(@Valid CategoryAddNewDto categoryAddNewDto) {
        log.debug("开始执行{}", categoryAddNewDto);
        categoryService.addNew(categoryAddNewDto);
        log.debug("添加完成");
        return JsonResult.ok();//静态方法
    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAnyAuthority('/category/delete')")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "id", value = "相册id", required = true, dataType = "Long")}
    )
    public JsonResult delete(@Range(min = 1, message = "Id异常，请添加正确的id") @RequestParam Long id) {
        categoryService.deleteById(id);
        return JsonResult.failed(ResponseCode.OK, "成功删除");
    }

    @GetMapping("/selectDetails")
    @ApiOperation("根据id查询分类详情")
    @ApiOperationSupport(order = 500)
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "Long")}
    )
    @PreAuthorize("hasAnyAuthority('/category/read')")
    public JsonResult selectByI(@Range(min = 1, message = "Id异常，请添加正确的id") @RequestParam Long id) {
        CategoryDetailsVo categoryDetailsVo = categoryService.selectDetailsById(id);
        return JsonResult.ok(categoryDetailsVo);
    }

    @PostMapping("/update")
    @ApiOperation("修改分类")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAnyAuthority('/category/update')")
    public JsonResult update(@Valid CategoryUpdateDto updateDto) {
        categoryService.updateById(updateDto);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("查询分类列表")
    @ApiOperationSupport(order = 600)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分类页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", required = true, dataType = "Integer")
    })
    public JsonResult<JsonPage<CategoryListVo>> list(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        log.debug("开始处理【查询分类列表】的请求，pageNum：{}，pageSize：{}", pageNum, pageSize);
        JsonPage<CategoryListVo> jsonPage = categoryService.list(pageNum, pageSize);
        return JsonResult.ok(jsonPage);
    }

    @GetMapping("/treeList")
    @ApiOperation("查询所有列表树")
    @ApiOperationSupport(order = 700)
    public JsonResult<List<TreeListVo>> allList() {
        List<TreeListVo> treeListVos = categoryService.treeList();
        return JsonResult.ok(treeListVos);
    }

    @PostMapping("defaultList")
    @ApiOperation("修改栏内的默认值")
    @ApiOperationSupport(order = 800)
    @PreAuthorize("hasAnyAuthority('/category/read')")
    @ApiImplicitParam(name = "id", value = "当前修改的id", required = true, dataType = "Long")
    public JsonResult<List<Long>> defaultList(@RequestParam Long id) {
        return JsonResult.ok(categoryService.defaultList(id));
    }

    @GetMapping("getIdAndName")
    @ApiOperation("获取分类id和名字")
    @ApiOperationSupport(order = 900)
    @PreAuthorize("hasAnyAuthority('/category/read')")
    @ApiImplicitParam(name = "id", value = "当前修改的id", required = true, dataType = "Long")
    public JsonResult<List<CategoryIdNameList>> getIdAndName(@RequestParam Long id){
        List<CategoryIdNameList> list = categoryService.getIdAndName(id);
        return JsonResult.ok(list);
    }

    @GetMapping("getFrontIdAndName")
    @ApiOperation("用户查询所有场地用的分类id和名字")
    @ApiOperationSupport(order = 1000)
    public JsonResult<List<CategoryIdNameList>> getFrontIdAndName(){
        List<CategoryIdNameList> list = categoryService.getFrontIdAndName();
        return JsonResult.ok(list);
    }

}
